<template>
  <div class="homes">
    <!-- 头部 -->
    <header class="home-header wrap" :class="{'active' : headerActive}" :style="style">
      <i class="iconfont icon-caidan"></i>
      <div class="header-search">
        <span class="app-name">G</span>
        <i class="iconfont icon-search"></i>
        <span class="search-title">家电返场同价11.11</span>
      </div>
      <!-- <router-link to="/login" tag="span" v-if="!isLogin">登录</router-link> -->
      <router-link v-if="!$store.getters.getIsLogin" tag="span" to="/login">登录</router-link>
      <router-link tag="span" to="./user" v-else>
        <i class="iconfont icon-iconyonghu"></i>
      </router-link>
    </header>
    <!-- 轮播图 -->
    <van-swipe :autoplay="3000" indicator-color="white" class="swiper-container">
      <van-swipe-item v-for="item in sliderList" :key="item.categoryId">
        <img :src="item.imgUrl" class="swiper_img" alt />
      </van-swipe-item>
    </van-swipe>
    <!-- 分类 -->
    <section class="category-list">
      <div v-for="(item,index) in categoryList" :key="index" @click="goproduct(item.categoryId)">
        <img :src="item.imgUrl" />
        <span>{{item.name}}</span>
      </div>
    </section>
    <!-- 楼层 -->
    <section class="floor-list">
      <div class="floor-wrap" v-for="(item1,index) in floorList" :key="index">
        <img :src="item1.headUrl" class="floor-head" />
        <div class="floor-content">
          <div class="floor-category" v-for="(item2,index) in item1.list" :key="index">
            <p>{{item2.title}}</p>
            <p>{{item2.desc}}</p>
            <div class="floor-products">
              <img :src="item3.imgUrl" v-for="(item3,index) in item2.products" :key="index" />
            </div>
          </div>
        </div>
      </div>
    </section>
    <tab-bar :active="0"></tab-bar>
  </div>
</template>

<script>
import TabBar from "../../components/tabBar.vue";
// 导入首页 网络请求
import { getSliders, homeCategory, homefloorlist } from "../../api";

export default {
  data() {
    return {
      style: { backgroundColor: "" },
      headerActive: false,
      scrollTop: 0,
      sliderList: [],
      categoryList: [],
      floorList: [],
    };
  },
  components: {
    TabBar,
  },
  created() {
    getSliders((res) => {
      this.sliderList = res.data;
    });
    homeCategory((res) => {
      this.categoryList = res.data;
    });
    homefloorlist((res) => {
      this.floorList = res.data;
    });
  },
  methods: {
    //顶部 屏幕滑动改变样式
    pageScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      if (scrollTop > 100) {
        this.style.backgroundColor = "#F63515";
        this.headerActive = true;
      } else if (scrollTop == 0) {
        this.style.backgroundColor = "transparent";
        this.headerActive = false;
      }
    },
    goproduct(e) {
      this.$router.push("/product-list/" + e);
    },
  },
  mounted() {
    window.addEventListener("scroll", this.pageScroll);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.pageScroll);
  },
};
</script>

<style lang="scss" scoped>
@import "../../commin/style/mixin";
//头部
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  @include fj;
  width: 100%;
  height: 100px;
  line-height: 100px;
  padding: 0 30px;
  @include boxSizing;
  font-size: 30px;
  color: #fff;
  z-index: 10000;
  background: transparent;

  .icon-caidan {
    font-size: 50px;
  }

  .header-search {
    display: flex;
    width: 74%;
    height: 40px;
    line-height: 40px;
    margin: 20px 0;
    padding: 10px 0;
    color: #232326;
    background: #fff;
    @include borderRadius(40px);

    .app-name {
      padding: 0 20px;
      color: $red;
      font-size: 40px;
      font-weight: bold;
      border-right: 1px solid #666;
    }

    .icon-search {
      padding: 0 20px;
      font-size: 34px;
    }

    .search-title {
      font-size: 24px;
      color: #666;
    }
  }

  .icon-iconyonghu {
    font-size: 44px;
  }
}
//轮播图
.swiper-container {
  .swiper_img {
    width: 100%;
  }
}

.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 26px;

  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;

    img {
      width: 80px;
      height: 80px;
      margin: 26px auto 16px auto;
    }
  }
}
.floor-list {
  width: 100%;
  padding-bottom: 100px;
  .floor-head {
    width: 100%;
    height: 80px;
    background: #f6f6f6;
  }

  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    @include boxSizing;

    .floor-category {
      width: 50%;
      padding: 20px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      @include boxSizing;

      &:nth-child(2n) {
        border-right: none;
      }

      p {
        font-size: 34px;
        color: #333;

        &:nth-child(2) {
          padding: 10px 0;
          font-size: 26px;
          color: $red;
        }
      }

      .floor-products {
        display: flex;
        justify-content: space-around;
        width: 100%;

        img {
          width: 130px;
          height: 130px;
        }
      }
    }
  }
}
</style>